#! /usr/bin/env python3
# -*- coding: utf8 -*-

'定制类：斐波那契数列'


class Fib(object):
    def __init__(self):
        self.num_a = 0
        self.num_b = 1

    # 类长度
    def __len__(self):
        return 0

    # 用于打印类
    def __str__(self):
        return str(self.__age)
    __repr__ = __str__

    # 类循环
    def __iter__(self):
        return self

    def __next__(self):
        self.num_a, self.num_b = self.num_b, self.num_a + self.num_b
        if(self.num_a > 10000):
            raise StopIteration
        return self.num_a

    # 类根据index取值  切片负数的情况仍未处理
    def __getitem__(self, index):
        self.num_a, self.num_b = 0, 1
        if isinstance(index, int):
            if(index > 20):
                raise StopIteration
            for x in range(index):
                self.num_a, self.num_b = self.num_b, self.num_a + self.num_b
            return self.num_a
        if isinstance(index, slice):
            start = index.start
            stop = index.stop
            step = index.step
            if start is None:
                start = 0
            if stop is None:
                raise TypeError("参数错误")
            listL = []
            n = 0
            for x in range(stop):
                if(x > 20):
                    raise StopIteration
                self.num_a, self.num_b = self.num_b, self.num_a + self.num_b
                if x >= start:
                    if step is None:
                        listL.append(self.num_a)
                    else:
                        if n == 0:
                            listL.append(self.num_a)
                            n = step
                        else:
                            n = n - 1
            return listL

    # 当python未找到参数时，将会调用getattr函数进行查询
    def __getattr__(self, name):
        return name

    def __call__(self):
        return "可调用"


# 开始调用
f = Fib()
for i in f:
    print(i)
print(f[:10:2])
print(f.sdadsad)
print(f(), callable(f))
